package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.CardListBean.CardBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.net._HttpRequest;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 银行卡——新增／修改
 * 
 * @author qunlee_mr
 * 
 */
public class UserBalanceCardAddAct extends BaseActivity {

	@ViewInject(id = R.id.card_commit)
	private Button btn_commit;

	@ViewInject(id = R.id.card_bank)
	private EditText bank;

	@ViewInject(id = R.id.card_name)
	private EditText name;

	@ViewInject(id = R.id.card_code)
	private EditText code;

	private CardBean cardBean;

	@ViewInject(id = R.id.is_default)
	private CheckBox is_default;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_card_add);
	}

	private boolean isDel = false;

	@Override
	public void init() {
		btn_commit = (Button) findViewById(R.id.card_commit);

		cardBean = (CardBean) getIntent().getSerializableExtra("card");
		if (cardBean == null) {
			getHeader().setTitle("新增银行卡");
			btn_commit.setText("新增");
			isDel = false;
			btn_commit.setEnabled(false);
		} else {
			isDel = true;
			getHeader().setTitle("修改银行卡");
			btn_commit.setText("修改");
			bank.setText("" + cardBean.bankName);
			bank.setEnabled(false);
			name.setText("" + cardBean.cardName);
			name.setEnabled(false);
			code.setText("" + cardBean.cardNumber);
			code.setEnabled(false);
			is_default.setChecked(cardBean.is_default.equals("1"));
			if (cardBean.is_default.equals("1")) {
				is_default.setEnabled(false);
			}
			btn_commit.setEnabled(true);
			btn_commit.setText("删除");
		}

		bank.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				checkData();
			}
		});
		name.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				checkData();
			}
		});
		code.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				checkData();
			}
		});

		btn_commit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isDel) {
					new DelCard(UserBalanceCardAddAct.this);
				} else {
					new UpdateCard(UserBalanceCardAddAct.this);
				}
			}
		});

		is_default.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isDel) {
					if (isChecked) {
						new SetDefault(UserBalanceCardAddAct.this);
					}
				}
			}
		});

	}

	private void checkData() {
		if (isDel)
			return;
		String bankStr = bank.getText().toString();
		if (bankStr == null || bankStr.equals("")) {
			btn_commit.setEnabled(false);
			return;
		}
		String nameStr = name.getText().toString();
		if (nameStr == null || nameStr.equals("")) {
			btn_commit.setEnabled(false);
			return;
		}
		String codeStr = code.getText().toString();
		if (codeStr == null || codeStr.equals("")) {
			btn_commit.setEnabled(false);
			return;
		}
		btn_commit.setEnabled(true);
	}

	@Override
	public void setData() {
	}

	/************************** 接口相关 ***********************************/
	/**
	 * 新增银行卡
	 */
	class UpdateCard extends WKAsyncTaskPro {
		public UpdateCard(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			String bankStr = bank.getText().toString();
			String nameStr = name.getText().toString();
			String codeStr = code.getText().toString();
			String isD = is_default.isChecked() ? "1" : "0";
			return app().net.updateBankCard(nameStr, codeStr, bankStr, isD);
		}

		// cardName 持卡人姓名
		// cardNumber 卡号
		// bankName 银行名
		// is_default 是否设为默认

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				IApplication.showToast(cardBean == null ? "新增成功" : "修改成功");
				finish();
			} else {
				Toast.makeText(ctx, "获取数据失败", Toast.LENGTH_SHORT).show();
			}
		}
	}

	/**
	 * 删除银行卡
	 */
	class DelCard extends WKAsyncTaskPro {
		public DelCard(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.delBankCard(cardBean.id);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				IApplication.showToast("删除成功");
				finish();
			} else {
				Toast.makeText(ctx, "获取数据失败", Toast.LENGTH_SHORT).show();
			}
		}
	}

	/**
	 * 设置默认
	 */
	class SetDefault extends WKAsyncTaskPro {
		public SetDefault(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.setDefault(cardBean.id);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				IApplication.showToast("设置成功");
				is_default.setEnabled(false);
			} else {
				Toast.makeText(ctx, "数据异常", Toast.LENGTH_SHORT).show();
			}
		}
	}

}
